/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.controller;

import com.extension.client.ExtensionWS;
import com.extension.client.ExtensionWS_Service;
import com.extension.client.IpccEXTENSIONGROUPS;
import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import com.ipcc.manager.entity.IPCC_EXTENSION_GROUPS;
import com.ipcc.webservice.model.ExtensionWSModel;
import java.io.Serializable;
import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;
import telsoft.admin.util.AppServer;
import telsoft.cms_admin.login.LoginBean;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class IPCC_EXTENSIONSController implements Serializable {
    
    private String oldPassword;
    private IPCC_EXTENSIONS current = new IPCC_EXTENSIONS();
    private LazyDataModel<IPCC_EXTENSIONS> lazyIEModel;
    private List<IPCC_EXTENSION_GROUPS> listEG = new ArrayList<IPCC_EXTENSION_GROUPS>();
    private List<IPCC_EXTENSIONS> listIE = new ArrayList<IPCC_EXTENSIONS>();
    private String extensionS;
    com.extension.client.ExtensionWS_Service service = null;
    com.extension.client.ExtensionWS app = null;
    private boolean is_admin = false;
    private String user_id = "";

    /**
     * Creates a new instance of IPCC_EXTENSIONSController
     */
    public IPCC_EXTENSIONSController() {
        try {
            is_admin = (Boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.IS_ADMIN);
            if (is_admin) {
                listIE = IPCC_EXTENSIONS.getAll("", null);
            } else {
                user_id = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.USER_ID).toString();
                listIE = IPCC_EXTENSIONS.getAll("", user_id);
            }
            
            lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
            listEG = current.getIpcc_extension_groups().getAll("");
            service = new com.extension.client.ExtensionWS_Service(new URL(getWSDL_ExtensionWS()));
            app = service.getExtensionWSPort();
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
    }
    
    public String getWSDL_ExtensionWS() {
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String wsdl = resource.getString("ExtensionWS_Service_WSDL");
        return wsdl;
    }
    
    public LazyDataModel<IPCC_EXTENSIONS> getLazyIEModel() {
        return lazyIEModel;
    }
    
    public void setLazyIEModel(LazyDataModel<IPCC_EXTENSIONS> lazyIEModel) {
        this.lazyIEModel = lazyIEModel;
    }
    
    public ExtensionWS_Service getService() {
        return service;
    }
    
    public void setService(ExtensionWS_Service service) {
        this.service = service;
    }
    
    public ExtensionWS getApp() {
        return app;
    }
    
    public void setApp(ExtensionWS app) {
        this.app = app;
    }
    
    public List<IPCC_EXTENSIONS> getListIE() {
        return listIE;
    }
    
    public void setListIE(List<IPCC_EXTENSIONS> listIE) {
        this.listIE = listIE;
    }
    
    public String getOldPassword() {
        return oldPassword;
    }
    
    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }
    
    public String getExtensionS() {
        return extensionS;
    }
    
    public void setExtensionS(String extensionS) {
        this.extensionS = extensionS;
    }
    
    public List<IPCC_EXTENSION_GROUPS> getListEG() {
        return listEG;
    }
    
    public void setListEG(List<IPCC_EXTENSION_GROUPS> listEG) {
        this.listEG = listEG;
    }
    
    public IPCC_EXTENSIONS getCurrent() {
        return current;
    }
    
    public void setCurrent(IPCC_EXTENSIONS current) {
        this.current = current;
    }
    
    public String groupNameByid() {
        try {
            return current.getIpcc_extension_groups().getNameById();
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public boolean checkInput() {
        if (current.getExtension() == null || current.getExtension().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không được để trống trường Extension!"));
            return false;
        }
        if (current.getPassword() == null || current.getPassword().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không được để trống trường Password!"));
            return false;
        }
        return true;
    }
    
    public String prepareInsert() {
        current = new IPCC_EXTENSIONS();
        return "";
    }
    
    public String insert() {
        try {
            if (checkInput()) {
                if (current.insert()) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                    listIE.add(current);
                    lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public String remove(IPCC_EXTENSIONS ie) {
        try {
            if (ie.getStatus() == 2) {
                disapprove();
            }
            if (ie.remove()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));
                listIE.remove(ie);
                lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public String prepareEdit(IPCC_EXTENSIONS ie) {
        try {
            current = (IPCC_EXTENSIONS) ie.clone();
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public String update() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            if (checkInput()) {
                if (current.update(cn)) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                    for (int i = 0; i < listIE.size(); i++) {
                        if (listIE.get(i).getId().equals(current.getId())) {
                            listIE.set(i, current);
                            break;
                        }
                    }
                    lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        } finally {
            current = new IPCC_EXTENSIONS();
            Database.closeObject(cn);
        }
        return "";
    }
    
    public String search() {
        try {
            if (is_admin) {
                listIE = current.getAll(" AND UPPER(F.EXTENSION) LIKE '%" + extensionS.toUpperCase() + "%'", null);
            } else {
                listIE = current.getAll(" AND UPPER(F.EXTENSION) LIKE '%" + extensionS.toUpperCase() + "%'", user_id);
            }
            lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
            int i = listIE.size();
            if (i > 0) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có " + i + " bản ghi được tìm thấy!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy"));
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
        }
        return "";
    }
    
    public String prepareApprove(IPCC_EXTENSIONS ie) {
        try {
            current = (IPCC_EXTENSIONS) ie.clone();
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public String approve() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            cn.setAutoCommit(false);
            current.setStatus(2);
            if (ExtensionWSModel.checkFileExists(current.getExtension() + ".xml")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File extension này đã tồn tại!"));
                return "";
            }
            if (current.update(cn)) {
                com.extension.client.IpccEXTENSIONS ie = new com.extension.client.IpccEXTENSIONS();
                ie.setId(current.getId());
                ie.setExtension(current.getExtension());
                IpccEXTENSIONGROUPS g = new IpccEXTENSIONGROUPS();
                g.setGroup(current.getIpcc_extension_groups().getGroup());
                ie.setIpccExtensionGroups(g);
                ie.setPassword(current.getPassword());
                ie.setStatus(current.getStatus());
                String msg = app.createXMLExtension(ie);
                if (msg.equals("Thành công")) {
                    for (int i = 0; i < listIE.size(); i++) {
                        if (listIE.get(i).getId().equals(current.getId())) {
                            listIE.set(i, current);
                            break;
                        }
                    }
                    lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                    cn.commit();
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        } finally {
            Database.closeObject(cn);
        }
        return "";
    }
    
    public String disapprove() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            cn.setAutoCommit(false);
            current.setStatus(0);
            if (!ExtensionWSModel.checkFileExists(current.getExtension() + ".xml")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File extension này không tồn tại!"));
                return "";
            }
            if (current.update(cn)) {
                com.extension.client.IpccEXTENSIONS ie = new com.extension.client.IpccEXTENSIONS();
                ie.setId(current.getId());
                ie.setExtension(current.getExtension());
                IpccEXTENSIONGROUPS g = new IpccEXTENSIONGROUPS();
                g.setGroup(current.getIpcc_extension_groups().getGroup());
                ie.setIpccExtensionGroups(g);
                ie.setPassword(current.getPassword());
                ie.setStatus(current.getStatus());
                String msg = app.deleteXMLExtension(ie.getExtension() + ".xml");
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
                if (msg.equals("Thành công")) {
                    for (int i = 0; i < listIE.size(); i++) {
                        if (listIE.get(i).getId().equals(current.getId())) {
                            listIE.set(i, current);
                            break;
                        }
                    }
                    lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                    cn.commit();
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        } finally {
            Database.closeObject(cn);
        }
        return "";
    }
    
    public String prepareChangePwd(IPCC_EXTENSIONS ie) {
        oldPassword = "";
        try {
            current = (IPCC_EXTENSIONS) ie.clone();
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        }
        return "";
    }
    
    public boolean checkChangePwd() {
        if (!oldPassword.equals(current.getPassword())) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mật khẩu cũ không chính xác!"));
            return false;
        }
        return true;
    }
    
    public String changePwd() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            cn.setAutoCommit(false);
            if (!ExtensionWSModel.checkFileExists(current.getExtension() + ".xml")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File extension này không tồn tại!"));
                return "";
            }
            if (checkChangePwd()) {
                if (current.update(cn)) {
                    com.extension.client.IpccEXTENSIONS ie = new com.extension.client.IpccEXTENSIONS();
                    ie.setId(current.getId());
                    ie.setExtension(current.getExtension());
                    IpccEXTENSIONGROUPS g = new IpccEXTENSIONGROUPS();
                    g.setGroup(current.getIpcc_extension_groups().getGroup());
                    ie.setIpccExtensionGroups(g);
                    ie.setPassword(current.getPassword());
                    ie.setStatus(current.getStatus());
                    String msg = app.editPasswordXMLExtension(ie);
                    if (msg.equals("Thành công")) {
                        for (int i = 0; i < listIE.size(); i++) {
                            if (listIE.get(i).getId().equals(current.getId())) {
                                listIE.set(i, current);
                                break;
                            }
                        }
                        lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                        cn.commit();
                    } else {
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
                    }
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        } finally {
            Database.closeObject(cn);
        }        
        return "";
    }
    
    public String changePwdFromDuyet() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            cn.setAutoCommit(false);
            if (!ExtensionWSModel.checkFileExists(current.getExtension() + ".xml")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File extension này không tồn tại!"));
                return "";
            }
            if (current.update(cn)) {
                com.extension.client.IpccEXTENSIONS ie = new com.extension.client.IpccEXTENSIONS();
                ie.setId(current.getId());
                ie.setExtension(current.getExtension());
                IpccEXTENSIONGROUPS g = new IpccEXTENSIONGROUPS();
                g.setGroup(current.getIpcc_extension_groups().getGroup());
                ie.setIpccExtensionGroups(g);
                ie.setPassword(current.getPassword());
                ie.setStatus(current.getStatus());
                String msg = app.editPasswordXMLExtension(ie);
                if (msg.equals("Thành công")) {
                    for (int i = 0; i < listIE.size(); i++) {
                        if (listIE.get(i).getId().equals(current.getId())) {
                            listIE.set(i, current);
                            break;
                        }
                    }
                    lazyIEModel = new LazyAbstractDataModel<IPCC_EXTENSIONS>(listIE);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                    cn.commit();
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
        } finally {
            Database.closeObject(cn);
        }
        return "";
    }
}
